package com.csthink.common.infrastructure.exception;

import com.csthink.common.infrastructure.enums.ErrorEnum;
import com.csthink.common.infrastructure.util.ErrorMessageUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-09
 */
@Getter
@Slf4j
public class BaseException extends RuntimeException implements Serializable {

    private static final long serialVersionUID = -4884152355863639835L;

    protected ErrorEnum errorEnum;
    protected String errorMsg;

    public BaseException(ErrorEnum errorEnum) {
        super(String.valueOf(errorEnum));
        this.errorEnum = errorEnum;
        this.errorMsg = ErrorMessageUtil.getMessage(errorEnum.getErrorCode());
    }

    public BaseException(ErrorEnum errorEnum, Object errorMsg) {
        super(String.format("%s - %s", errorEnum, errorMsg));
        this.errorEnum = errorEnum;
        this.errorMsg = String.valueOf(errorMsg);
    }

    public BaseException(ErrorEnum errorEnum, Object errorMsg, Throwable throwable) {
        super(String.format("%s - %s", errorEnum, errorMsg), throwable);
        this.errorEnum = errorEnum;
        this.errorMsg = String.valueOf(errorMsg);
    }

    public int getErrCode() {
        return this.errorEnum.getErrorCode();
    }

}
